package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.R;
import com.itheima.entity.Employee;
import com.itheima.entity.Permissions;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.service.EmployeeService;
import com.itheima.service.PermissionsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PermissionsService permissionsService;
    //员工登录
    public R<Employee> login(HttpServletRequest request,Employee employee){
        //数据库的密码被MD5加密，所以这里获取到的的密码需要加密
        String password=employee.getPassword ();
        password=DigestUtils.md5DigestAsHex (password.getBytes ());
        //查询数据库，看获得的数据与数据库中的数据是否一致
        LambdaQueryWrapper<Employee> qw=new LambdaQueryWrapper<> ();
        qw.eq(Employee::getUsername,employee.getUsername());
        //返回一条数据
        Employee emp=employeeMapper.selectOne (qw);
        //比较用户名
        if(emp==null){
            return R.error ("登录失败");
        }
        //校验密码
        if(!emp.getPassword ().equals (password)){
            return R.error ("密码错误，登录失败");
        }
        //状态
        if(emp.getStatus ()==0){
            return R.error ("该账号已被禁用");
        }
        //登录成功，将员工id存入Session并返回登录成功结果
        request.getSession ().setAttribute ("employee",emp.getId ());
        // 登录成功后，获取权限信息，并保存到session 中
        List<Permissions> permissions = permissionsService.getPermissions(emp.getId());
        // 登录的如果是admin，则赋予所有的权限
        if (emp.getUsername().equals("admin")) {
            Permissions per = new Permissions();
            per.setMethodType("*");
            per.setUrl("/**");
            permissions.add(per);
        }
        request.getSession().setAttribute("permissions", permissions);
        return R.success (emp);

    }

    //增加员工
    public R<String>add(HttpServletRequest request,@RequestBody Employee employee){
        employee.setPassword (DigestUtils.md5DigestAsHex ("123456".getBytes ()));
//        employee.setCreateTime (LocalDateTime.now ());
//        employee.setUpdateTime (LocalDateTime.now ());
        //获得当前用户的id
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser (empId);
//        employee.setUpdateUser (empId);
        employeeMapper.insert (employee);
        return R.success ("增加成功");
    }
    //根据id查询
    public R<Employee> getById(@PathVariable Long id){
        Employee byId=employeeMapper.selectById(id);
        if(byId!=null){
            return R.success (byId);
        }
        return R.error ("没有找到对应的员工信息");
    }
    //修改状态
    public R<String>update(HttpServletRequest request,@RequestBody Employee employee){
        //获得当前修改的用户的id
        // Long employeeId=(Long) request.getSession ().getAttribute ("employee");
        // employee.setUpdateTime (LocalDateTime.now ());
        // employee.setUpdateUser (employeeId);
        employeeMapper.updateById (employee);
        return R.success ("员工信息修改成功");
    }
    //根据名字分页查询
    public R<Page> page(int page, int pageSize, String name){
       //分页构造器
        Page pageInfo=new Page(page, pageSize);
        //条件查询构造器
        LambdaQueryWrapper<Employee> qw=new LambdaQueryWrapper<> ();
        //名字不为空，为条件
        qw.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //根据修改时间降序
        qw.orderByDesc (Employee::getUpdateTime);
        employeeMapper.selectPage (pageInfo,qw);
        return R.success (pageInfo);
    }

}
